﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevelopStudio.Control.Basic;
using DevelopStudio.Basic;
using System.IO;

namespace DevelopStudio.Control.FormPage
{
    public partial class CodeReviewForm : ChangeListFormBase
    {
        public AppEnum.ChangelistStatus NextChangelistStatus { get; set; }

        private CodeReviewForm()
        {
            InitializeComponent();
        }

        public CodeReviewForm(ChangelistInfo changelistInfo)
            : base(changelistInfo)
        {
            InitializeComponent();
            this.Text += " - " + this.ChangelistInfo.Name;
        }

        protected bool CheckValid(out string message)
        {
            message = string.Empty;

            if (string.IsNullOrEmpty(this.richTextBoxFeedback.Text) == false &&
                (this.richTextBoxFeedback.Text.Contains('[') ||
                this.richTextBoxFeedback.Text.Contains(']')))
            {
                message = Properties.Resources.ErrorComment;
                return false;
            }

            SearchBugs MyBugs = Utility.GetSearchBugs(Utility.Settings.MyInfo.ID);
            List<BugInfo> thisBugs = MyBugs.Bugs.Where(o => this.ChangelistInfo.BugInfos.Any(a => a.ID == o.ID) == true).ToList();

            foreach (BugInfo var in this.ChangelistInfo.BugInfos)
            {
                BugInfo temp = thisBugs.Find(o => o.ID == var.ID);

                if (temp == null)
                {
                    message = string.Format(Properties.Resources.ErrorNotYourBugs, var.ID, Properties.Resources.Unknown);
                    return false;
                }

                if (Utility.SupportCodeReviewBugs(temp, out message) == false ||
                    Utility.IsMyBugs(temp, Utility.Settings.MyInfo.ID, out message) == false ||
                    Utility.IsCodeReviewBug(temp, Utility.Settings.MyInfo.ID, out message) == false)
                {
                    return false;
                }
            }

            this.ChangelistInfo.BugInfos.Clear();
            this.ChangelistInfo.BugInfos.AddRange(thisBugs);

            return true;
        }

        private void buttonApprove_Click(object sender, EventArgs e)
        {
            string message;

            if (this.CheckValid(out message) == false)
            {
                MessageBox.Show(message);
                this.ChangelistInfo.ErrorInfo = new ChanglistErrorInfo(AppEnum.ChangelistErrorCode.CodeReview, message);
                this.DialogResult = System.Windows.Forms.DialogResult.None;
                return;
            }

            this.ChangelistInfo.FeedbackComment = this.richTextBoxFeedback.Text;

            if (string.IsNullOrEmpty(this.ChangelistInfo.FeedbackComment) == true)
            {
                this.NextChangelistStatus = AppEnum.ChangelistStatus.Approved;
            }
            else
            {
                this.NextChangelistStatus = AppEnum.ChangelistStatus.ApprovedWithComment;
            }

            this.ProcessStatusChange();
        }

        private void ProcessStatusChange()
        {
            foreach (BugInfo var in this.ChangelistInfo.BugInfos)
            {
                var.AssignedTo = new IdNamePair() { ID = this.ChangelistInfo.SendedBy.ID, Name = this.ChangelistInfo.SendedBy.Name };

                if (var.UpdateRedmine(this.ChangelistInfo, this.NextChangelistStatus) == false)
                {
                    MessageBox.Show(string.Format(Properties.Resources.ErrorUpdateBugs, var.ID));
                    this.DialogResult = System.Windows.Forms.DialogResult.None;
                    return;
                }
            }

            this.ChangelistInfo.Status = (int)this.NextChangelistStatus;
        }

        private void buttonReject_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.richTextBoxFeedback.Text) == true)
            {
                MessageBox.Show(Properties.Resources.ErrorRejectCommentEmpty);
                this.DialogResult = System.Windows.Forms.DialogResult.None;
                return;
            }

            string message;

            if (this.CheckValid(out message) == false)
            {
                MessageBox.Show(message);
                this.ChangelistInfo.ErrorInfo = new ChanglistErrorInfo(AppEnum.ChangelistErrorCode.CodeReview, message);
                this.DialogResult = System.Windows.Forms.DialogResult.None;
                return;
            }

            this.ChangelistInfo.FeedbackComment = this.richTextBoxFeedback.Text;

            this.NextChangelistStatus = AppEnum.ChangelistStatus.Rejected;
            this.ProcessStatusChange();
        }

        private void buttonView_Click(object sender, EventArgs e)
        {
            string execFileFullPath = Path.Combine(Application.StartupPath, AppConst.DiffToolPath);
            string fileFullName = this.ChangelistInfo.StoredPath;
            Utility.ExecuteCmd(execFileFullPath, fileFullName);
        }

        private void CodeReviewForm_Load(object sender, EventArgs e)
        {
            foreach (BugInfo bugInfo in this.ChangelistInfo.BugInfos)
            {
                this.listView1.Items.Add(new ListViewItem(new string[] { bugInfo.ID.ToString(), bugInfo.Subject, bugInfo.Description }) { Tag = bugInfo });
            }

            this.textBoxComment.Text = this.ChangelistInfo.Comment;
        }

    }
}
